﻿// ***********************************************************************
// Assembly         : YERIL_PI_INT
// Author           : Diego Bonilla
// Created          : 03-02-2014
//
// Last Modified By : Diego Bonilla
// Last Modified On : 03-02-2014
// ***********************************************************************
// <copyright file="Estructura.aspx.cs" company="Development Solutions CR">
//     Copyright (c) Development Solutions CR. All rights reserved.
// </copyright>
// <summary></summary>
// ***********************************************************************
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

/// <summary>
/// The Login namespace.
/// </summary>
namespace YERIL_PI_INT.PaginasWebs.Modulos.Login
{
    /// <summary>
    /// Class Estructura.
    /// </summary>
    public partial class Estructura : System.Web.UI.Page
    {
        /// <summary>
        /// The form accion
        /// </summary>
        static string FormAccion = "";
        /// <summary>
        /// The form retorno
        /// </summary>
        static string FormRetorno = "";
        /// <summary>
        /// The mensaje error
        /// </summary>
        public string MensajeError = "";
        /// <summary>
        /// The codigo
        /// </summary>
        static int codigo = 0;
        /// <summary>
        /// Handles the Load event of the Page control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="EventArgs"/> instance containing the event data.</param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Session["FormAccion"] != null)
            {
                FormAccion = Session["FormAccion"].ToString();

            }

            if (Session["FormRetorno"] != null)
            {
                FormRetorno = Session["FormRetorno"].ToString();

            }

            if (Session["Codigo"] != null)
            {
                codigo = Convert.ToInt32(Session["Codigo"]);

            }

            if (!IsPostBack)
            {

                if (FormAccion == "Editar")
                {
                    if (codigo != 0)
                    {
                        CargarEditar();
                    }
                }

            }

        }

        /// <summary>
        /// Cargars the editar.
        /// </summary>
        private void CargarEditar()
        {
            WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
            wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
            WSGestion.MOD_Estructura objEstructura = new WSGestion.MOD_Estructura();
            objEstructura = wsgest.SEL_EstructuraByID(codigo);
            txtEstructura.Text = objEstructura.EST_nombre;
            txtDescripcion.Text = objEstructura.EST_descripcion;

        }

        /// <summary>
        /// Handles the Click event of the btnSalir control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnSalir_Click(object sender, ImageClickEventArgs e)
        {
            Response.Redirect("GridEstructuras.aspx");
        }

        /// <summary>
        /// Handles the Click event of the btnAgregar control.
        /// </summary>
        /// <param name="sender">The source of the event.</param>
        /// <param name="e">The <see cref="ImageClickEventArgs"/> instance containing the event data.</param>
        protected void btnAgregar_Click(object sender, ImageClickEventArgs e)
        {
            try
            {
                if (FormAccion == "Agregar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_Estructura Estructura = new WSGestion.MOD_Estructura();
                    Estructura.EST_nombre = txtEstructura.Text;
                    Estructura.EST_descripcion = txtDescripcion.Text;

                    int cant_clie = wsgest.SEL_EstructuraByNombre(txtEstructura.Text);
                    if (cant_clie <= 0)
                    {
                        if (wsgest.CRE_Estructura(Estructura))
                        {
                            if (FormRetorno != "OrdenamientoEstructural.aspx")
                            {
                                Response.Redirect("GridEstructuras.aspx");
                            }
                            else
                            {
                                Response.Redirect("OrdenamientoEstructural.aspx");
                            }
                            
                        }
                        else
                        {
                            MensajeError = "Procedimiento Almacenado no aceptado";
                        }
                    }
                    else
                    {
                        MensajeError = "El Subtipo " + txtEstructura.Text + " ya existe";
                    }

                }
                if (FormAccion == "Editar")
                {
                    WSGestion.WSGestion wsgest = new WSGestion.WSGestion();
                    wsgest.Url = ConfigurationManager.AppSettings.Get("dirWs");
                    WSGestion.MOD_Estructura Estructura = new WSGestion.MOD_Estructura();
                    Estructura.EST_codestructura = codigo;
                    Estructura.EST_nombre = txtEstructura.Text;
                    Estructura.EST_descripcion = txtDescripcion.Text;

                    wsgest.UPD_Estructura(Estructura);
                    Response.Redirect("GridEstructuras.aspx");

                }

            }
            catch (Exception exc)
            {

                MensajeError = exc.ToString();
            }
        }
    }
}